package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.mapper.LectureMapper;
import com.srzk.youthleagueback.pojo.CommuPlat;
import com.srzk.youthleagueback.pojo.Lecture;
import com.srzk.youthleagueback.pojo.Meeting;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;
import static com.srzk.youthleagueback.results.CodeMsg.NOT_EXIST;

@Service
public class LectureService {
    @Autowired
    private LectureMapper mapper;

    public Result<Map<String,Long>> addLecture(Lecture lecture) {
        int result = mapper.insert(lecture);
        if (result == 1) {
            HashMap<String,Long> map = new HashMap<>();
            map.put("id",lecture.getId());
            return Result.success(map);
        } else {
            return Result.error(ADD_FAIL);
        }
    }
    public Result<Map<String,Object>> getGradeList(String grade, String year) {
        List<Lecture> allData = mapper.selectByGrade(year);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();

            allData.forEach(items -> {
                List<Map<String, Object>> list = new ArrayList<>();
                if (ObjectUtil.getGrade(items.getUser()).equals(grade)) {
                    String getClass = ObjectUtil.getClass(items.getUser());

                    Map<String,Object> listItem = new HashMap<>();
                    listItem.put("date",items.getDate());
                    listItem.put("id",items.getId());
                    listItem.put("address",items.getAddress());
                    listItem.put("theme",items.getTheme());
                    listItem.put("mainContent",items.getMainContent());
                    listItem.put("count",items.getCount());

                    boolean flag = false;
                    for (Map<String, Object> stringObjectMap : data) {
                        if (getClass.equals(stringObjectMap.get("class"))) {
                            ArrayList tempList = (ArrayList) stringObjectMap.get("list");
                            tempList.add(listItem);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        Map<String, Object> dataItem = new HashMap<>();
                        dataItem.put("class", getClass);
                        list.add(listItem);
                        dataItem.put("list", list);
                        data.add(dataItem);
                    }
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getHistoryList(String user) {
        List<Lecture> allData = mapper.selectAll(user);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();
            allData.forEach(items -> {
                ArrayList<Map<String, Object>> list;

                Map<String,Object> listItem = new HashMap<>();
                listItem.put("date",items.getDate());
                listItem.put("id",items.getId());
                listItem.put("address",items.getAddress());
                listItem.put("theme",items.getTheme());
                listItem.put("mainContent",items.getMainContent());
                listItem.put("count",items.getCount());

                boolean flag = false;
                for (Map<String, Object> datum : data) {
                    if(items.getYear().equals(datum.get("year"))){
                        list = (ArrayList<Map<String, Object>>) datum.get("list");
                        list.add(listItem);
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    list = new ArrayList<>();
                    Map<String, Object> dataItem = new HashMap<>();
                    dataItem.put("year", items.getYear());
                    list.add(listItem);
                    dataItem.put("list", list);
                    data.add(dataItem);
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<String> deleteLecture(String user, String year, int id) {
        int result = mapper.deleteByUserAndYearAndTypeAndId(user, year, id);
        if (result == 1) {
            return Result.success("删除成功");
        } else {
            return Result.error(DELETE_FAIL);
        }
    }

    public Result<String> updateLecture(Lecture lecture) {
        int result = mapper.updateByPrimaryKey(lecture);
        if (result == 1) {
            return Result.success("更新成功");
        } else {
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<List<Lecture>> getLectureList(String user, String year) {
        List<Lecture> meeting = mapper.selectByPrimaryKeyList(user, year);
        if (meeting != null) {
            return Result.success(meeting);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Lecture> getLecture(String user, String year, int id) {
        Lecture lecture = mapper.selectByPrimaryKey(id, user, year);
        if (lecture != null) {
            return Result.success(lecture);
        }
        return Result.error(NOT_EXIST);
    }
}
